<?php
//To understand recursion, see the bottom of this file

class Automat extends CI_Controller {

	public function Automat()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->model('Automatmodel');
                $this->load->model('Logmodel');
                $this->load->model('Usermodel');
                $this->load->model('Waremodel');
                $this->load->library('session');
	}
	
	public function index()
	{
            
            $username = $this->session->userdata('username');
            if($username){
                $this->load->model('Usermodel');
                $userId = $this->Usermodel->getUserIdByLogin($username);
                $this->load->model('Automatmodel');
                $viewData['automates'] = $this->Automatmodel->GetAllAutomats($userId);
                $viewData['username'] = $username;
                
                $this->load->view('new_automat_list_view', $viewData);
            }
            else{
                $this->load->view('access_denined_view', null);
            }
	}
	
        public function GetAutomateDetails(){
            $username = $this->session->userdata('username');
            if(!$username){
                $this->load->view('access_denined_view', null);
                return;
            }
            $automatId = $_POST['automatId'];
            $viewData['log'] = $this->Logmodel->GetAutomatLog($automatId);
            $viewData['wares'] = $this->Waremodel->GetAutomatWaresExt($automatId);
            $viewData['automate'] = $this->Automatmodel->getAutomat($automatId);
            $this->load->view('automat_details_ajax_view', $viewData);
        }
        
        public function DeleteAutomat(){
            $username = $this->session->userdata('username');
            if(!$username){
                return;
            }
            $automatId = $_GET['automatId'];
            echo 'ID::'.$automatId;
            $this->Automatmodel->deleteAutomat($automatId);
        }
		
        //this func is called by automate and returns new automate ID
        public function RegisterAutomat(){
            
            if(!$this->Usermodel->loginPassValid($_GET['username'], $_GET['password'])){
                return -1;
            }
            $userId = $this->Usermodel->getUserIdByLogin($_GET['username']);
            $res = $this->Automatmodel->addAutomat(date('Y-m-d H:i:s'), 
                                                $_GET['latitude'], 
                                                $_GET['longitude'], 
                                                $_GET['ip'], 
                                                $userId, 
                                                $_GET['balance'], 
                                                $_GET['serviceability'],
                                                $_GET['info']);
            return $res;
        }
        
        //login, password, automatId, wareId
        public function SellWare(){
            if (!isset($_GET['login']) ||
                !isset($_GET['password']) ||
                !isset($_GET['automatId']) ||
                !isset($_GET['wareId'])
                    ) {
                return;
            }
            if (!$this->Usermodel->loginPassValid($_GET['login'],$_GET['password'])) {
                return;
            }
            $userId = $this->Usermodel->getUserIdByLogin($_GET['login']);
            $ware = $this->Waremodel->getWare($_GET['wareId']);
            if ($ware['Amount'] < 1) {
                return;
            }
            $automat = $this->Automatmodel->getAutomat($_GET['automatId']);
            if ($automat['Serviceability'] < 1) {
                return;
            }
            $this->Waremodel->updateWare($_GET['wareId'], null, $ware['Amount']-1, null, null, null, null);
            $this->Automatmodel->setBalance($_GET['automatId'], $automat['Balance'] + $ware['Price']);
            $this->Logmodel->log($_GET['automatId'], $_GET['wareId']);
        }
	
	// Function for simulator
	public function simSellWare() {
		if (!isset($_POST['login']) ||
            !isset($_POST['password']) ||
            !isset($_POST['automatId']) ||
            !isset($_POST['wareId'])) {
            return;
        }
		$username = $_POST['login'];
		$password = $_POST['password'];
		$automatId = $_POST['automatId'];
		$wareId = $_POST['wareId'];
        if (!$this->Usermodel->loginPassValid($username, $password)) {
            return;
        }
        $userId = $this->Usermodel->getUserIdByLogin($username);
        $ware = $this->Waremodel->getWare($wareId);
        if ($ware['Amount'] < 1) {
            return;
        }
        $automat = $this->Automatmodel->getAutomat($automatId);
        if ($automat['Serviceability'] < 1) {
            return;
        }
        $this->Waremodel->updateWare($wareId, null, $ware['Amount']-1, null, null, null, null);
        $this->Automatmodel->setBalance($automatId, $automat['Balance'] + $ware['Price']);
        $this->Logmodel->log($automatId, $wareId);
	}
	
	// Function for simulator
	public function simRegisterAutomat() {
		if (!isset($_POST['login']) ||
            !isset($_POST['password']) ||
            !isset($_POST['latitude']) ||
			!isset($_POST['longitude']) ||
			!isset($_POST['ip']) ||
			!isset($_POST['balance']) ||
			!isset($_POST['serviceability']) ||
			!isset($_POST['info'])) {
            return;
        }
		
		$username = $_POST['login'];
		$password = $_POST['password'];
		
		if (!$this->Usermodel->loginPassValid($username, $password)) {
            return;
        }
		
		$userId = $this->Usermodel->getUserIdByLogin($username);
		
		$res = $this->Automatmodel->addAutomat(date('Y-m-d H:i:s'), 
			$_POST['latitude'], $_POST['longitude'], $_POST['ip'], 
			$userId, $_POST['balance'], $_POST['serviceability'], $_POST['info']);
		
		echo "@".$this->Automatmodel->lastAutomatId()."@";
	}
	
	// Function for simulator
	public function simGetAutomatWares() {
		if (!isset($_POST['login']) ||
            !isset($_POST['password']) ||
            !isset($_POST['automatId'])) {
            return;
        }
		
		$username = $_POST['login'];
		$password = $_POST['password'];
		$automatId = $_POST['automatId'];
		
		if (!$this->Usermodel->loginPassValid($username, $password)) {
            return;
        }
			
		$automat = $this->Automatmodel->getAutomat($automatId);
		if (!$automat) {
			return;
		}
		// It means that automat exists
		echo "OK";
		echo "@".$automat['AutomatId']."@".$automat['Balance'];
		$wares = $this->Waremodel->getAutomatWares($automatId);
		foreach ($wares as &$ware) {
			echo "@".$ware['ProductCode']."@".$ware['Amount']."@".$ware['Price'];
			echo "@".$ware['WareId']."@".$ware['CanSell']."@".$ware['SellFrom']."@".$ware['SellTo'];
		}
	}        
}
//To understand recursion, see the top of this file